package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private  DeptMapper deptMapper;
    //3. 添加部门
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
    //1. 部门列表查询
    @Override
    public PageResult<Dept> list(String name, Integer status, Integer page, Integer pageSize) {
        //1.设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接方法，执行查询
        List<Dept> deptList = deptMapper.selectList(name, status);
        //3.解析并封装成PageResult返回分页结果
        Page<Dept> p = (Page<Dept>) deptList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
    //4. 根据ID查询
    @Override
    public Dept getById(Integer id) {
        return deptMapper.selectById(id);
    }
    //5. 修改部门
    @Override
    public void update(Dept dept) {
        //1.补全基础属性
        dept.setUpdateTime(LocalDateTime.now());
        //2.调用mapper
        deptMapper.update(dept);
    }
    //2. 删除部门
    @Override
    public void deleteById(Integer id) {
         deptMapper.deleteById(id);
    }
    //6. 查询所有部门
    @Override
    public List<Dept> list() {
        return deptMapper.selectAll();
    }
}
